﻿using System;
using System.Collections.Generic;

namespace WcfAdvice.Except
{
    /// <summary>
    /// 服务异常
    /// </summary>
    /// <typeparam name="T">类型参数 （Type Parameter）</typeparam>
    public class SrvException<T> : Exception
    {
        public int ErrCode { get; private set; }
        public string ErrMessage { get; private set; }

        public SrvException() { }

        public SrvException(ResponseCode errorCode)
        {
            ErrCode = (int)errorCode;
            ErrMessage = EnumHelper.GetDescription(errorCode);

            //base(errMessage);
        }

        public SrvException(Enum errorCode)
        {
            if (!typeof(T).IsEnum)
            {
                throw new ArgumentException("T must be an enumerated type");
            }
            T t = default(T);
            string aa = Enum.GetName(typeof(T), errorCode);
            //if (Enum.TryParse(errorCode, out t))
            //{

            //}
            //ErrCode = (int)errorCode;
            ErrMessage = EnumHelper.GetDescription(errorCode);
        }

        public SrvException(int errorCode, string message)
            : base(message)
        {
            ErrCode = errorCode;
            ErrMessage = message;
        }

        public SrvException(int errorCode, string message, Exception innerException)
            : base(message, innerException)
        {
            ErrCode = errorCode;
            ErrMessage = message;
        }

        public override string ToString() { return string.Format("[{0}] {1}", ErrCode, Message); }
    }
}
